/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AuthPenSign;

import AuthPenSign.Database.*;
import AuthPenSign.Signature.*;
import AuthPenSign.GUI.*;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JTextField;
import jpen.owner.multiAwt.AwtPenToolkit;

/**
 *
 * @author V-ray
 * Это главный класс приложения.
 */
public class AuthPenSign {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		new AuthPenSign();
	}
	MainFrame mainFrame;
	OutFrame outFrame;
	Database db;
	PenAdapter pa;
	ArrayList<Point> arrayPoints;
	Signature sign;

	/**
	 * Производит инициализацию.
	 */
	public AuthPenSign() {
		this.mainFrame = new MainFrame();
		this.outFrame = new OutFrame();
		this.outFrame.setDefaultCloseOperation(OutFrame.HIDE_ON_CLOSE);
		this.db = new Database();

		this.pa = new PenAdapter();
		this.pa.AddOutput((Graphics2D) this.mainFrame.jPanel1.getGraphics());
		this.arrayPoints = new ArrayList<>();
		this.pa.AddOutput(this.arrayPoints);
		JTextField[] textFields = {this.mainFrame.jTextField1, this.mainFrame.jTextField2, this.mainFrame.jTextField3};
		this.pa.AddOutput(textFields);

		this.mainFrame.jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton1MouseClicked(evt);
			}
		});
		this.mainFrame.jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton2MouseClicked(evt);
			}
		});
		this.mainFrame.jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton3MouseClicked(evt);
			}
		});

		AwtPenToolkit.addPenListener(this.mainFrame.jPanel1, this.pa);
		this.mainFrame.setVisible(true);
	}

	private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
		this.sign = new Signature(this.arrayPoints);
		this.db.WriteSignature(this.sign);
	}

	private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
		if (this.sign == null) {
			this.sign = new Signature(this.arrayPoints);
		}
		this.outFrame.setVisible(true);
		this.outFrame.DrawSignature(this.sign);
	}

	private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
		this.sign = new Signature(this.arrayPoints);

		Signature sign_2 = this.db.ReadSignature();

		this.outFrame.setVisible(true);
		this.outFrame.DrawSignature(this.sign, sign_2);
		this.sign.Equals(sign_2);
	}
}
